import { Store } from 'redux';
import { IAllAction, IAllState } from '../interfaces';

type StoreType = Store<IAllState, IAllAction>;
type CombineStoreType = Store<
    {
        allState: IAllState;
    },
    IAllAction
>;
type AllStoreType = CombineStoreType | StoreType;
type StoreStateType = IAllState | { allState: IAllState };

let _store: AllStoreType;
let _useCombineReducers = false;

// 视图层调用的store初始化
const setStore = (createdStore: AllStoreType, useCombineReducers = false) => {
    _store = createdStore;
    _useCombineReducers = useCombineReducers;
};
const getStore = () => _store;
const isUseCombineReducers = () => _useCombineReducers;
const getAllStates = (state: StoreStateType): IAllState => {
    if (_useCombineReducers && 'allState' in state) {
        return state.allState;
    } else {
        return state as IAllState;
    }
};

export { getStore, setStore, isUseCombineReducers, StoreStateType, getAllStates };
